package org.jeecgframework.codegenerate.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import cn.hutool.core.util.StrUtil;

public class AesUtil {

 public static String convertBigInteger(byte[] bytes, int radix) {//
     return (new BigInteger(1, bytes)).toString(radix);
 }

 public static String encodeBase64(byte[] src) {//a
     return Base64.getEncoder().encodeToString(src);
 }

 public static byte[] decodeStr(String encodeStr) throws Exception {//a
     return StrUtil.isEmpty(encodeStr) ? null : Base64.getDecoder().decode(encodeStr);
 }

 public static byte[] md5(byte[] var0) throws Exception {//b
     MessageDigest md5 = MessageDigest.getInstance("MD5");
     md5.update(var0);
     return md5.digest();
 }

 public static byte[] md5(String str) throws Exception {//b
     return StrUtil.isEmpty(str) ? null : md5(str.getBytes());
 }

 public static String encodeMd5(String str) throws Exception {//c
     return StrUtil.isEmpty(str) ? null : encodeBase64(md5(str));
 }

 
 public static byte[] aesEncrypt(String input, String seed) throws Exception {//a
     KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
     SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
     random.setSeed(seed.getBytes());
     keyGenerator.init(128, random);
     Cipher cipher = Cipher.getInstance("AES");
     cipher.init(1, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));
     return cipher.doFinal(input.getBytes("utf-8"));
 }

 public static String encodeEncrypt(String input, String seed) throws Exception {//b
     return encodeBase64(aesEncrypt(input, seed));
 }

 public static String aesEncrypt(byte[] input, String seed) throws Exception {//a
     KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
     SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
     random.setSeed(seed.getBytes());
     keyGenerator.init(128, random);
     Cipher cipher = Cipher.getInstance("AES");
     cipher.init(2, new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES"));
     return new String(cipher.doFinal(input));
 }

 public static String aesEncodedCatch(String encodeStr, String seed) throws Exception {//c
     return StrUtil.isEmpty(encodeStr) ? null : aesEncrypt(decodeStr(encodeStr), seed);
 }

 public static String aesEncoded(String encodeStr, String seed) {//d
     try {
         return StrUtil.isEmpty(encodeStr) ? null : aesEncrypt(decodeStr(encodeStr), seed);
     } catch (Exception var3) {
         return "CC ERROR";
     }
 }
}
